All Questions
16 questions
1vote
1answer
192views
Is it an acceptable pattern to put derived classes inside an abstract base class in Java?
Suppose I have some Java code such as the following (in this case, the use of the name "interaction" is referring to interacting with an object in a video game): public abstract class ...
1vote
2answers
1kviews
Concept/Design question: Alternatives to switch/conditional statements and Enums
I am practicing design patterns and OO concepts such as inheritance in java and I'm writing an application that represents a vending machine. I have two questions focused on ideal structure and design ...
2votes
1answer
334views
Composition or Inheritance for classes with almost similar implementations but different input and outputs for methods?
I have the following classes, which have quite similar method implementations. Only the classes' method inputs and outputs seem to be of different types. When I put it like this, it sounds like a case ...
28votes
7answers
10kviews
Polymorphism case study - Design pattern for morphing?
Imagine two classes: class Person { MarriedPerson marry(Person other) {...} } class MarriedPerson extends Person { } The idea is that a Person is automatically "morphed" to type MarriedPerson ...
10votes
5answers
4kviews
Is it ever okay to violate the LSP?
I'm following up on this question, but I'm switching my focus from code to a principle. From my understanding of the Liskov substitution principle (LSP), whatever methods are in my base class, they ...
4votes
1answer
5kviews
Designing generic type inheritance hierarchy
I have now put another rephrased version of original question as requested by the user in the comments with class names mimicking real world scenario of postal office (though I dont know how real ...
4votes
2answers
2kviews
Inheritance and factory together?
I have a hierarchical data model and am trying to implement their CRUD operations in my Web Application. Currently I have code inheritance for CRUD operations of my entities (resources) as follows: ...
2votes
3answers
205views
Data duplication, can it be an unavoidable practice in this example?
Say I have different employees of type Employee stored in a list inside a class SubCase. public class SubCase{ protected ArrayList<Employee> employees; ... } SubCase represents a part ...
1vote
2answers
246views
Processing and sending processed data to super from child class constructor
I want to do some initialization in child class constructor and pass result to super(). But Java doesn't allow any processing in child class constructor before super() call. Whats a good way to ...
1vote
1answer
224views
Pattern to share fields in inheritance
I'm trying to figure out the best way to solve a design issue. I have to be able to clone (I'm cloning them to Apache HttpClient) different types of HttpServletRequest (POST, GET...) and then send ...
11votes
5answers
3kviews
Separate interface for mutation methods
I've been working on refactoring some code, and I think I may have taken the first step down the rabbit hole. I'm writing the example in Java, but I suppose it could be agnostic. I have an interface ...
3votes
4answers
6kviews
Implements > extends, but what about variables?
It's preferable to write programs that depend on interfaces rather than on superclasses, but what if you want a class to have certain variables? Sometimes you want a class to implement a certain ...
6votes
3answers
1kviews
How to refactor my design, if it seems to require multiple inheritance?
Recently I made a question about Java classes implementing methods from two sources (kinda like multiple inheritance). However, it was pointed out that this sort of need may be a sign of a design flaw....
3votes
5answers
2kviews
Explanation of the definition of interface inheritance as described in GoF book
I am reading the first chapter of the Gof book. Section 1.6 discusses about class vs interface inheritance: Class versus Interface Inheritance It's important to understand the difference between an ...
3votes
2answers
1kviews
Template method within one class without subclasses or inheritance
I have an algorithm with mostly invariant parts that needs to be reused within one class so as to stay DRY. Code duplication with repeating method structure public void save(String key, int value) {...